package com.softtown.modules.dapai.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.modules.dapai.dao.CompAfterSaleDao;
import com.softtown.modules.dapai.dto.CompAfterSaleAPPDTO;
import com.softtown.modules.dapai.dto.CompAfterSaleDTO;
import com.softtown.modules.dapai.dto.CompAfterSaleAddDTO;
import com.softtown.modules.dapai.dto.CompServiceOrderAPPDTO;
import com.softtown.modules.dapai.entity.CompAfterSaleAPPEntity;
import com.softtown.modules.dapai.entity.CompAfterSaleDetailEntity;
import com.softtown.modules.dapai.entity.CompAfterSaleEntity;
import com.softtown.modules.dapai.entity.CompServiceOrderAPPEntity;
import com.softtown.modules.dapai.service.CompAfterSaleAPPService;
import com.softtown.modules.dapai.service.CompAfterSaleDetailService;
import com.softtown.modules.dapai.service.CompAfterSaleService;
import com.softtown.modules.dapai.service.CompServiceOrderAPPService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @Author: Ding Ya Yong
* @Description: 售后申请单
* @date: 2022/1/2 11:38
* @Param:
* @return:
*/

@Service
public class CompAfterSaleServiceImpl extends
        BaseServiceImpl<CompAfterSaleDao, CompAfterSaleEntity> implements CompAfterSaleService {


    @Resource
    private CompAfterSaleDetailService compAfterSaleDetailService;
    @Resource
    private CompAfterSaleAPPService compAfterSaleAPPService;
    @Resource
    private CompServiceOrderAPPService compServiceOrderAPPService;


    @Override
    public PageInfo<CompAfterSaleDTO> getAfterSalePageList(CompAfterSaleDTO compAfterSaleDTO) {
        Integer pageSize = compAfterSaleDTO.getPageSize();
        Integer page = compAfterSaleDTO.getPage();
        if (pageSize > 0 && page > 0) {
            PageHelper.startPage(page, pageSize);
        }
        List<CompAfterSaleDTO> compAfterSaleDTOList =
                baseDao.getAfterSalePageList(compAfterSaleDTO.getCompId(), compAfterSaleDTO.getParam());
        return  new PageInfo<>(compAfterSaleDTOList);
    }

    @Override
    public CompAfterSaleAddDTO getAfterSaleById(Long compId, Long id) {
        CompAfterSaleAddDTO compAfterSaleAddDTO = new CompAfterSaleAddDTO();
        compAfterSaleAddDTO.setCompAfterSaleEntity(baseDao.getAfterSaleById(compId, id));
        compAfterSaleAddDTO.setCompAfterSaleDetailEntityList(baseDao.getAfterSaleDetailList(compId, id));
        return compAfterSaleAddDTO;
    }

    @Override
    @Transactional
    public void addAfterSale(CompAfterSaleAddDTO CompAfterSaleAddDTO) {
        CompAfterSaleEntity compAfterSaleEntity =ConvertUtils
                .sourceToTarget(CompAfterSaleAddDTO.getCompAfterSaleEntity(), CompAfterSaleEntity.class);
        insert(compAfterSaleEntity);
        CompAfterSaleAddDTO.getCompAfterSaleDetailEntityList().forEach(item ->{
            item.setAfterSaleId(compAfterSaleEntity.getId());
            item.setCompId(compAfterSaleEntity.getCompId());
        });
        compAfterSaleDetailService.insertBatch(ConvertUtils
                .sourceToTarget(CompAfterSaleAddDTO.getCompAfterSaleDetailEntityList(), CompAfterSaleDetailEntity.class));

    }

    @Override
    public PageInfo<CompAfterSaleAPPDTO> getAfterSalePageListAPP(CompAfterSaleAPPDTO compAfterSaleAPPDTO) {
        Integer pageSize = compAfterSaleAPPDTO.getPageSize();
        Integer page = compAfterSaleAPPDTO.getPage();
        if (pageSize > 0 && page > 0) {
            PageHelper.startPage(page, pageSize);
        }
        List<CompAfterSaleAPPDTO> compAfterSaleAPPDTOList =
                baseDao.getAfterSalePageListAPP(compAfterSaleAPPDTO.getCompId(), compAfterSaleAPPDTO.getParam());
        return  new PageInfo<CompAfterSaleAPPDTO>(compAfterSaleAPPDTOList);
    }

    @Override
    public CompAfterSaleAPPDTO getAfterSaleByIdAPP(Long compId, Long id) {
        CompAfterSaleAPPDTO compAfterSaleAPPDTO = baseDao.getAfterSaleByIdAPP(compId, id);
        return compAfterSaleAPPDTO;
    }

    @Override
    @Transactional
    public void addAfterSaleAPP(CompAfterSaleAPPEntity compAfterSaleAPPEntity) {
        compAfterSaleAPPService.insert(compAfterSaleAPPEntity);
    }


    @Override
    @Transactional
    public void disAfterSaleAPP(CompServiceOrderAPPEntity compServiceOrderAPPEntity) {
        compServiceOrderAPPService.insert(compServiceOrderAPPEntity);
    }


    @Override
    @Transactional
    public void updateAfterSale(CompAfterSaleAddDTO CompAfterSaleAddDTO) {
        CompAfterSaleEntity compAfterSaleEntity =ConvertUtils
                .sourceToTarget(CompAfterSaleAddDTO.getCompAfterSaleEntity(), CompAfterSaleEntity.class);
        updateById(compAfterSaleEntity);
        baseDao.deleteDetail(compAfterSaleEntity.getCompId(), compAfterSaleEntity.getId());
        CompAfterSaleAddDTO.getCompAfterSaleDetailEntityList().forEach(item ->{
            item.setAfterSaleId(compAfterSaleEntity.getId());
            item.setCompId(compAfterSaleEntity.getCompId());
        });
        compAfterSaleDetailService.insertBatch(ConvertUtils
                .sourceToTarget(CompAfterSaleAddDTO.getCompAfterSaleDetailEntityList(), CompAfterSaleDetailEntity.class));
    }

    @Override
    public void deleteAfterSale(CompAfterSaleDTO compAfterSaleDTO) {
        baseDao.deleteAfterSale(compAfterSaleDTO.getCompId(), compAfterSaleDTO.getId());
        baseDao.deleteDetail(compAfterSaleDTO.getCompId(), compAfterSaleDTO.getId());
    }
}
